package com.sl.au.service.impl;

import com.sl.au.entity.News;
import com.sl.au.entity.User;
import com.sl.au.repository.NewsRepository;
import com.sl.au.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("NewsService")
@Transactional
public class NewsServiceImpl extends BaseServiceImpl implements NewsService {

	@Autowired
	NewsRepository newsRepository;
	@Override
	public List<News> getPage(int start, int length, String key, String orderName, String dir) {
		Page<News> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"createTime");
		pages=newsRepository.findAll(pageable);
		return pages.getContent();
	}
	@Override
		 public List<News> getNews(int start, int length,Map<String,String> condition,String orderName ,String dir){
		Page<News> pages = null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
		String dataFilter = condition.get("dataFilter");
		String key=condition.get("key");
		String userId = condition.get("userId");
		String superior=condition.get("superior");
		String areaId=condition.get("areaId");
		if(dataFilter.equals("本部门")){
			pages = newsRepository.getNewsTitle(key,superior,userId, pageable);
		}
		else if(dataFilter.equals("本片区")){
			pages = newsRepository.getAreaNewsTitle(key, areaId,userId, pageable);
		}
		else if(dataFilter.equals("所有")){
//			pages = newsRepository.getNewsTitle(key, pageable);
			pages = newsRepository.getAllNewsTitle(key,userId, pageable);
		}
		else{
			pages = newsRepository.getNewsUser(key,userId, pageable);
		}
//		pages = newsRepository.getNewsUser(key,userId, pageable);
		return pages.getContent();
	}
	@Override
	public List<News> getIndex(int start, int length,Map<String,String> condition,String orderName ,String dir){
		Page<News> pages = null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
		String key=condition.get("key");
		String superior=condition.get("superior");
		pages = newsRepository.getNewsTitle(key,superior, pageable);
		return pages.getContent();
	}
	@Override
	public NewsRepository getRepository() {
		return newsRepository;
	}
	@Override
	public Long getNewsCount(Map<String, String> condition) {
		String dataFilter = (String)condition.get("dataFilter");
		Long count =(long) 0;
		
		if(dataFilter.equals("本部门")){
			count =newsRepository.getNewsCount(condition.get("key"), condition.get("superior"),condition.get("userId"));
		}
		else if(dataFilter.equals("所有")){
			count = newsRepository.getAllNewsCount(condition.get("key"), condition.get("userId"));
		}
		else if(dataFilter.equals("本片区")){
			count = newsRepository.getAreaNewsCount(condition.get("key"), condition.get("areaId"), condition.get("userId"));
		}
		else{
			count =newsRepository.getNewsUserCount(condition.get("key"), condition.get("userId"));
		}
//		count =newsRepository.getNewsUserCount(condition.get("key"), condition.get("userId"));
		return count;
	}
//	@Override
//	public List<News> getIndex(int start, int length, String key, String orderName, String dir) {
//		// TODO Auto-generated method stub
//		Page<News> pages = null;
//		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
//		pages = newsRepository.getNewsTitle(key, pageable);
//		return pages.getContent();
//	}
	@Override
	public Long getIndexCount(Map<String, String> condition) {
		Long count =(long) 0;
		count =newsRepository.getNewsCount(condition.get("key"), condition.get("superior"));
		return count;
	}

}